package Service.impl;

import Mapper.ClientMapper;
import Mapper.OrderMapper;
import Mapper.UserMapper;
import Pojo.*;
import Service.OrderService;
import Util.BaseMapper;
import Util.SnowFlakeUtil;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import Util.AliPayUtil2;
import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    private OrderMapper orderMapper= BaseMapper.getSqlSession().getMapper(OrderMapper.class);
    private ClientMapper clientMapper=BaseMapper.getSqlSession().getMapper(ClientMapper.class);
    private UserMapper userMapper=BaseMapper.getSqlSession().getMapper(UserMapper.class);
    @Override
    public Integer AddOne(Order order) {
        //订单编号生成
        try {
            String orderNumber= String.valueOf(SnowFlakeUtil.getID());
            order.setOrder_number(orderNumber);
            order.setOrder_status_id(3);
            //根据传入的用户id查询对应的手机号,再通过手机号去查询客户表是否有当前用户，有则将客户id添加至该订单行中
            User user=userMapper.QueryById(order.getUid());
            Client client=clientMapper.QueryByPhone(user.getUser_phone());
            if(client!=null){
                order.setClient_id(client.getClient_id());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderMapper.AddOne(order);
    }

    @Override
    public Map<Object, String> ZfbQrcread(Order order, OrderDetails orderDetails) {
        HashMap<Object,String> map=new HashMap<>();
        //创建商品对象
        Pay pay=new Pay();
        pay.setOut_trade_no(order.getOrder_number());
        pay.setScene("线上支付");
        pay.setSubject("房间预订");
        pay.setStore_id("dh233");
        pay.setTotal_amount(String.valueOf(orderDetails.getPrice_sum()));
        pay.setTimeout_express("3m");
        pay.setBody("");
        //调用工具类中的二维码创建方法
        AlipayF2FPrecreateResult result=AliPayUtil2.tradePrecreate(pay);
        switch (result.getTradeStatus()){
            case SUCCESS:
                AlipayTradePrecreateResponse response=result.getResponse();
                map.put("QrResult",response.getQrCode());
                map.put("orderNumber",order.getOrder_number());
                break;
            case FAILED:
                map.put("QrResult","创建失败");
                break;
            case UNKNOWN:
                map.put("QrResult","系统异常");
                break;
        }
        return map;
    }

    @Override
    public String QrStatusQuery(String orderNumber) {
        String s=AliPayUtil2.TestTradeQuery(orderNumber);
        //根据订单编号查询
        Order order=orderMapper.QueryByNumber(orderNumber);
        if(s.equals("success")){
            //修改支付状态
            order.setOrder_status_id(5);
            Integer integer=orderMapper.UpdateStatusById(order);
        }else {
            return "fail";
        }
        return "success";
    }

    @Override
    public Order OrderQueryById(Integer orderId) {
        return orderMapper.OrderQueryById(orderId);
    }


}
